package com.ydcycloud.company.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.smart.mvc.model.Pagination;
import com.smart.mvc.model.Result;
import com.smart.mvc.validator.Validator;
import com.smart.mvc.validator.annotation.ValidateParam;
import com.smart.util.StringUtils;
import com.ydcycloud.company.model.Company;
import com.ydcycloud.company.service.CompanyService;
import com.ydcycloud.sys.controller.BaseAdminController;

@Controller
@RequestMapping("/admin/company")
public class CompanyController extends BaseAdminController {

	@Resource
	private CompanyService companyService;

	/**
	 * 跳转到企业列表页面
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping
	public String excute(Model model) {
		return "/admin/company/companyList";
	}

	/**
	 * 企业列表
	 * 
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	@RequestMapping(path = "/list")
	public @ResponseBody
	Result list(
			@ValidateParam(name = "开始页码", validators = { Validator.NOT_BLANK }) Integer pageNo,
			@ValidateParam(name = "显示条数 ", validators = { Validator.NOT_BLANK }) Integer pageSize,
			String companyName) {
		Pagination<Map<String, Object>> p = companyService.getCompanyList(companyName, pageNo, pageSize);
		return Result.createSuccessResult().setData(p);
	}

	/**
	 * 删除企业信息
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping(value = "/delete")
	public @ResponseBody
	Result delete(@ValidateParam(name = "主键ids") String ids) {
		if (StringUtils.isBlank(ids)) {
			return Result.createErrorResult().setMessage("至少选择一行");
		}
		companyService.deleteByIds(ids);
		return Result.createSuccessResult().setMessage("删除成功");
	}

	/**
	 * 跳转到编辑/新增企业页面
	 * 
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET, value = "/edit")
	public String edit(Integer id, Model model) {
		if (id != null) {
			Company bean = companyService.get(id);
			model.addAttribute("bean", bean);
//			session.put("plist", plist);
		}
		model.addAttribute("lists", companyService.findTradeName());
		return "/admin/company/companyEdit";
	}

	/**
	 * 编辑新增公司信息
	 * @param request
	 * @param id
	 * @param companyName
	 * @param personCount
	 * @param addressDetail
	 * @param companyPhone
	 * @param logoUrl
	 * @param content
	 * @return
	 */
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public @ResponseBody
	Result save(HttpServletRequest request, Integer id, String companyName,Integer tradeId,
			Integer personCount, String addressDetail, String companyPhone,
			String logoUrl, String content) {

		if (StringUtils.isBlank(companyName)) {
			return Result.createErrorResult().setMessage("公司名字不能为空");
		}
		if (StringUtils.isBlank(companyPhone)) {
			return Result.createErrorResult().setMessage("公司联系方式不能为空");
		}
		Company bean = null;
		if (id != null) {
			bean = companyService.get(id);
			if (bean == null) {
				return Result.createErrorResult().setMessage("该图片不存在");
			}
		} else {
			bean = new Company();
			bean.setCreateBy(getSessionAdmin(request.getSession()).getName()); // 创建人
			bean.setCreateTime(new Date()); // 创建时间
		}
		bean.setCompanyName(companyName);
		bean.setPersonCount(personCount);
		bean.setAddressDetail(addressDetail);
		bean.setCompanyPhone(companyPhone);
		bean.setLogoUrl(logoUrl);
		bean.setContent(content);
		bean.setTradeId(tradeId);
		bean.setLastUpdateTime(new Date());
		companyService.save(bean);
		List<Company> com = new ArrayList<Company>();
//		com.add(bean);
//		request.setAttribute("bean", bean);
		return Result.createSuccessResult().setData(bean);
	}
}
